package org.jeecg.modules.backstageModule.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.FinanceModule.mapper.ReturnMoneyMapper;
import org.jeecg.modules.backstageModule.mapper.BackCustMapper;
import org.jeecg.modules.backstageModule.service.BackCustService;
import org.jeecg.modules.zonglzp.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class BackCustServiceImpl implements BackCustService {

    @Autowired
    ReturnMoneyMapper returnMoneyMapper;

    @Autowired
    BackCustMapper mapper;

    /*
    总后台客户管理查询
     */
    @Override
    public IPage<User> getBackCustList(Page<User> page, String realName, String phone, String starttime, String endtime, String companyName, String companyCode, int status) {
        IPage<User> pageList=mapper.getBackCustList(page, realName, phone,companyName,companyCode,status);

        if(pageList!=null){
            for(int i=0;i<pageList.getRecords().size();i++){
               //查询当前折扣体系名称
                String discountSystemName=mapper.getdiscountSystemNamebyuserId(pageList.getRecords().get(i).getDiscount());
                if(discountSystemName==null){
                    pageList.getRecords().get(i).setDiscountSystemName("暂无折扣体系");
                }else {
                    pageList.getRecords().get(i).setDiscountSystemName(discountSystemName);
                }

              String qujianxiaofeimoney= mapper.getQujianxiaofeimoneybyId(pageList.getRecords().get(i).getId(),starttime,endtime);
                System.out.println("都打印看一看"+qujianxiaofeimoney);

              if(qujianxiaofeimoney==null){
                  qujianxiaofeimoney="0.0";}
                pageList.getRecords().get(i).setQujianxiaofeimoney(qujianxiaofeimoney);
            }
        }
        return pageList;
    }

    @Override
    public User getBackCustDetail(int userId) {
        return mapper.getBackCustDetail(userId);
    }

    @Override
    public int stopCust(int userId, int status) {

        if(status==2){
            //通过userId查询公司Id
            int companyId=mapper.getcompanyIdbyuserId(userId);

            //通过userId查询公司编码
            String companyCode=mapper.getcompanyCodebyuserId(userId);

            //删除公司下所有门店
            mapper.deleteallstorebycompanyId(companyId);

            //清空公司下所有员工
            mapper.deleteallstaffbycompanyCode(companyCode);
            return   mapper.deleteuser(userId, status);
        }
        return mapper.stopCust(userId, status);
    }


    //设置月结额度
    @Override
    public int setUserMouth(int userId, int isMonth, String monthLines) {
        //获取当前月结额度
        String oldmonthLines=mapper.getoldmonthLines(userId);
        BigDecimal chaju=new BigDecimal(monthLines).subtract(new BigDecimal(oldmonthLines));

        //获取老的剩余
        String  oldremainingLimit=returnMoneyMapper.getoldremainingLimit(userId);
        BigDecimal shengyu=new BigDecimal(oldremainingLimit);
        //添加后修改剩余月结额度
        returnMoneyMapper.updateuserRemainingLimit(userId,String.valueOf( shengyu.add(chaju )  ));

        return mapper.setUserMouth(userId, isMonth, monthLines);
    }

    //总后台新增客户
    @Override
    public int addBackCustomer(User user) {
        return mapper.addBackCustomer(user);
    }


    //编辑用户信息
    @Override
    public int updateuser(User user) {
        //这里还要加入一部编辑公司表的信息


        //获取当前月结额度
        String oldmonthLines=mapper.getoldmonthLines(user.getId());
        BigDecimal chaju=new BigDecimal(user.getMonthLines()).subtract(new BigDecimal(oldmonthLines));

        //获取老的剩余
        String  oldremainingLimit=returnMoneyMapper.getoldremainingLimit(user.getId());
        BigDecimal shengyu=new BigDecimal(oldremainingLimit);
        //添加后修改剩余月结额度
        returnMoneyMapper.updateuserRemainingLimit(user.getId(),String.valueOf( shengyu.add(chaju )  ));


        mapper.updatecompanybyuserId(user);
        return mapper.updateuser(user);
    }


    //在插入user表后，往公司表里插入数据
    @Override
    public void addusertoCompanyTable(User user) {
        mapper.addusertoCompanyTable(user);
    }

    //通过账号查询这个user的Id
    @Override
    public int getuserIdbyusexrname(String username) {
        return mapper.getuserIdbyusexrname(username);
    }

    //通过公司编码查询到公司的id
    @Override
    public int getcompanyIdbycode(String wenzi) {
        return mapper.getcompanyIdbycode(wenzi);
    }

    //给新增的公司默认添加一个门店
    @Override
    public void addfirststore(String companyName, String currentTimeMillis, String address, Date date, int companyId, String phone, String realName) {
           mapper.addfirststore(companyName,currentTimeMillis,address,date,companyId,phone,realName);
    }

    //通过id查询用户
    @Override
    public User getuserbyId(int id) {
        return mapper.getuserbyId(id);
    }

    //通过公司id获取门店id
    @Override
    public int getstoreIdbycompanyId(int companyId) {
        return mapper.getstoreIdbycompanyId(companyId);
    }

    //将这个门店id 赋给用户
    @Override
    public void updateuserstoreId(int userId, int storeId) {
      mapper.updateuserstoreId(userId,storeId);
    }

    //给用户加上权限的编码
    @Override
    public void updateOrgCode(int userId, String upquanxian) {
        mapper.updateOrgCode(userId,upquanxian);
//        if(upquanxian==null || "".equals(upquanxian)){
//            mapper.updateOrgCode(userId,"D0"+String.valueOf(userId));
//        }else{
//            mapper.updateOrgCode(userId,upquanxian+"D0"+String.valueOf(userId));
//        }
    }
}
